package pe.gob.sedapal.scmultimedia.rcp.view.servicios;

import java.util.ArrayList;
import java.util.List;


import org.eclipse.jface.dialogs.MessageDialog;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;


import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseEvent;


import pe.gob.sedapal.scmultimedia.config.ApplicationContext;
import pe.gob.sedapal.scmultimedia.domain.Servicio;

import pe.gob.sedapal.scmultimedia.service.LogEstadisticoService;
import pe.gob.sedapal.scmultimedia.service.ServicioService;
import pe.gob.sedapal.scmultimedia.rcp.view.NavegacionView;
import pe.gob.sedapal.scmultimedia.util.Constante;
import pe.gob.sedapal.scmultimedia.util.Messages;
import pe.gob.sedapal.scmultimedia.wb.swt.ResourceManager;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.wb.swt.SWTResourceManager;

public class ConsultaServiciosView extends ViewPart {
	
	public static final String ID = Constante.ID_CONSULTA_SERVICIOS;
	//public static final String ID = "scmultimedia.consultaServicios";
	
	private List<Servicio> serviciosList = new ArrayList<Servicio>();
	private ServicioService servicioService;
	private LogEstadisticoService logEstadisticoService;

	private Table table;
	private Composite composite;
	private final Shell shell=new Shell(Display.getDefault(), SWT.NO_TRIM | SWT.ON_TOP);


	public ConsultaServiciosView() {
		servicioService= (ServicioService)ApplicationContext.getBean(ServicioService.class);
		logEstadisticoService= (LogEstadisticoService)ApplicationContext.getBean(LogEstadisticoService.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		
		composite = new Composite(parent, SWT.NONE);
		composite.setBackgroundImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_FONDO_SEDAPAL));
		composite.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setBounds(45, 145, 681, 248);
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);		
		
		Label imgLogo = new Label(composite, SWT.NONE);
		imgLogo.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_LOGO_SEDAPAL_INTERNO));
		imgLogo.setBounds(635, 10, 192, 84);		
		
		table = new Table(scrolledComposite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);	
		table.setHeaderVisible(false);
			
//		TableColumn tblclmnNombreServicio = new TableColumn(table, SWT.CENTER);
//		tblclmnNombreServicio.setWidth(15);
//		tblclmnNombreServicio.setText(Constante.NOMBRE_SERVICIO);
		scrolledComposite.setContent(table);
		scrolledComposite.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		
		Label lblTituloServicios = new Label(composite, SWT.NONE);
		lblTituloServicios.setForeground(org.eclipse.wb.swt.SWTResourceManager.getColor(51, 102, 153));
		lblTituloServicios.setFont(SWTResourceManager.getFont("FreeSans", 17, SWT.BOLD | SWT.ITALIC));
		lblTituloServicios.setBounds(10, 20, 703, 23);
		lblTituloServicios.setText(Messages.consultaServicios_titulo);
		
		Label imgBarralbl = new Label(composite, SWT.NONE);
		imgBarralbl.setImage(ResourceManager.getPluginImage("scmultimedia", Constante.RUTA_IMG_BARRA));
		imgBarralbl.setBounds(10, 61, 736, 8);
		
		Label lblElijaServicio = new Label(composite, SWT.NONE);
		lblElijaServicio.setBounds(328, 110, 199, 29);		
		lblElijaServicio.setForeground(SWTResourceManager.getColor(Constante.VALOR_FUENTE_RED,Constante.VALOR_FUENTE_GREEN, Constante.VALOR_FUENTE_BLUE));
		lblElijaServicio.setFont(SWTResourceManager.getFont("Ubuntu", 14, SWT.BOLD));		
		lblElijaServicio.setText(Messages.consultaServicios_subtitulo);
		
	    serviciosList =  servicioService.listarServicios();
		dibujarTabla();
		
		table.addMouseListener(new org.eclipse.swt.events.MouseListener(){
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				if(e.button ==1){

					int indice = table.getSelectionIndex();
					System.out.println("INDICE ES "+indice);
					
					IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					PdfView view;	
				  
					//OCULTAMOS LAS VISTAS	
					@SuppressWarnings("deprecation")
					IViewPart[] vistasActuales = pagina.getViews();
					
					for (IViewPart iViewPart : vistasActuales) {
						pagina.hideView(iViewPart);
					}
					
					
					Job job = new Job("Cargando pdf...") {
						@Override
						protected IStatus run(IProgressMonitor monitor) {
							doLongThing();
							//	syncWithUi(); mensaje luego que carga 
							// Use this to open a Shell in the UI thread
							return Status.OK_STATUS;
						}

					};
					job.setUser(true);
					job.schedule();
					
					
					
					//CARGAMOS EL PDF EN OTRA VISTA	
					
					try {
						view = (PdfView) pagina.showView(PdfView.ID);							
						//validamos qué PDF abrirá						
						if(indice == 0){
							view.openPDF(Constante.RUTA_PATH_PDF+Constante.RUTA_PDF_1);	
						}else if(indice == 1){
							view.openPDF(Constante.RUTA_PATH_PDF+Constante.RUTA_PDF_2);	
						}else{							
							view.openPDF(Constante.RUTA_PATH_PDF+Constante.RUTA_PDF_3);
						}
								
						view.zoom(3);
						pagina.showView(NavegacionView.ID);
						logEstadisticoService.ActualizarDatos("scMultimedia", 2111L, Constante.ACCION_SERVICIO);						
					} catch (PartInitException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			}
	
			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub				
			}
			
		});
		
		IWorkbenchPage pagina = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();		
		System.out.println("PDF 1: "+PdfView.ID);
		try {
			System.out.println("dentro del try 2 PDF 1: "+PdfView.ID);
			pagina.showView(PdfView.ID);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	public void dibujarTabla(){
		TableItem item, item1, item2, item3, item4;

//		for (Servicio centro : serviciosList) {
//			item= new TableItem(table, SWT.NONE);
//			System.out.println("columna "+centro.getNomUnicom());
//			item.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
//			item.setText(0, centro.getNomUnicom());		
//		}	
		
		item= new TableItem(table, SWT.NONE);
		item.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
		item.setText(0,Messages.etiqueta_requisito1);		
		
		
		item1= new TableItem(table, SWT.NONE);
		item1.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
		item1.setText(0,Messages.etiqueta_requisito2);		
		
		
		item2= new TableItem(table, SWT.NONE);
		item2.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
		item2.setText(0, Messages.etiqueta_requisito3);		
		
		
		item3= new TableItem(table, SWT.NONE);
		item3.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
		item3.setText(0, Messages.etiqueta_requisito4);		
		
		
		item4= new TableItem(table, SWT.NONE);
		item4.setFont(org.eclipse.wb.swt.SWTResourceManager.getFont("FreeSans", 15, SWT.NORMAL));
		item4.setText(0, "Requisitos para el Acceso a los servicios de Agua Potable y Alcantarillado ");		
		
		
		
		table.setRedraw(true);		
	}
	
	private void doLongThing() {
		for (int i = 0; i < 3; i++) {
			try {
				// We simulate a long running operation here
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("Doing something");
		}
	}
	

	@SuppressWarnings("unused")
	private void syncWithUi() {
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				MessageDialog.openInformation(shell, "PDF",
						Constante.CARGA_PDF_CORRECTA);
			}
		});

	}
}
